﻿using System;
using System.Xml;
using System.IO;
namespace GeoSketch2.Core
{
    /// <summary>
    /// Indicates the type of the vertex, at the begining of the section
    /// </summary>
    public enum VertexType
    {
        /// <summary>
        /// A line is after this vertex
        /// </summary>
        Line,
        /// <summary>
        /// An arc is after this vertex
        /// </summary>
        Arc
    }
    
    /// <summary>
    /// The vertex object used for polylines for example
    /// </summary>
    [Serializable]
    public class Vertex
    {
        #region XMLElementsName
#pragma warning disable 1591
        public const string XML_Position = "Position";
        public const string XML_Type = "Type";
#pragma warning restore 1591
        #endregion

        #region Fields
        /// <summary>
        /// The position of the vertex
        /// </summary>
        public Point2D64D Position;

        /// <summary>
        /// The type of vertex
        /// </summary>
        public VertexType Type; 
        #endregion

        #region Constructor
        /// <summary>
        /// The toolbar for image rectification
        /// </summary>
        /// <param name="Position">The position of the vertex</param>
        /// <param name="Type">The type of the vertex</param>
        public Vertex(Point2D64D Position, VertexType Type)
        {
            this.Position = Position;
            this.Type = Type;
        }

        /// <summary>
        /// The toolbar for image rectification
        /// </summary>
        /// <param name="Xr">The XmlReader to read from</param>
        public Vertex(XmlReader Xr)
        {
            #region Position
            Xr.ReadToFollowing(XML_Position);
            Xr.ReadToFollowing(typeof(Point2D64D).FullName);
            this.Position = new Point2D64D(Xr);
            #endregion

            #region Type
            Xr.ReadToFollowing(XML_Type);
            this.Type = (VertexType)Enum.Parse(typeof(VertexType), Xr.ReadString());
            #endregion

            Xr.ReadEndElement();
        }
        #endregion

        #region Method
        /// <summary>
        /// writes the object to a file
        /// </summary>
        /// <param name="Xw">The XnlWriter to write to</param>
        public void ToFile(XmlWriter Xw)
        {
            Xw.WriteStartElement(this.GetType().Name);

            #region Position
            Xw.WriteStartElement(XML_Position);
            this.Position.ToFile(Xw);
            Xw.WriteEndElement();
            #endregion

            #region Type
            Xw.WriteStartElement(XML_Type);
            Xw.WriteString(this.Type.ToString());
            Xw.WriteEndElement();
            #endregion

            Xw.WriteEndElement();
        }

        public void ToDXFR13(TextWriter Tw)
        {
            Tw.WriteLine("10");
            Tw.WriteLine(this.Position.X.ToString(Core.Vars.Setup.GeoSketch.NumberFormat));

            Tw.WriteLine("20");
            Tw.WriteLine(this.Position.Y.ToString(Core.Vars.Setup.GeoSketch.NumberFormat));

            Tw.WriteLine("30");
            Tw.WriteLine("0.0");
        }
        #endregion
    }
}
